home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Linux Cubed Series 8: LINUX Games
/
Linux Cubed Series 8 - LINUX Games.iso
/
games
/
x11
/
strategy
/
xpuzzles.3
/
xpuzzles
/
xpuzzles-5.3.1
/
xoct
/
xoct.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-02-05
|
12KB
|
421 lines
/*
# X-BASED OCTAHEDRON
#
# xoct.c
#
###
#
# Copyright (c) 1994 - 96 David Albert Bagley, bagleyd@hertz.njit.edu
#
# All Rights Reserved
#
# Permission to use, copy, modify, and distribute this software and
# its documentation for any purpose and without fee is hereby granted,
# provided that the above copyright notice appear in all copies and
# that both that copyright notice and this permission notice appear in
# supporting documentation, and that the name of the author not be
# used in advertising or publicity pertaining to distribution of the
# software without specific, written prior permission.
#
# This program is distributed in the hope that it will be "playable",
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
*/
/*
Version 4: 94/05/31 Xt
Version 3: 93/04/01 Motif
Version 2: 92/01/29 XView
Version 1: 91/03/19 SunView
*/
#include <stdlib.h>
#include <stdio.h>
#ifdef VMS
#include <unixlib.h>
#define getlogin cuserid
#else
#ifndef apollo
#include <unistd.h>
#endif
#endif
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Shell.h>
#include <X11/cursorfont.h>
#include "Oct.h"
#include "oct.xbm"
#ifndef SCOREFILE
#define SCOREFILE "/usr/games/lib/oct.scores"
#endif
/* The following are in OctP.h also */
#define MINOCTAS 1
#define PERIOD3 3
#define PERIOD4 4
#define BOTH 5
#define MAXMODES 3
#define MAXOCTAS 6
#define MAXRECORD 32767
#define MAXPROGNAME 80
#define MAXNAME 256
static void Initialize();
static void CallbackOct();
static void PrintRecord();
static int HandleSolved();
static void PrintState();
static void ReadRecords();
static void WriteRecords();
static Arg arg[5];
static int octRecord[MAXMODES][2][MAXOCTAS - MINOCTAS + 2], movesDsp = 0;
static char progDsp[64] = "xoct";
static char recordDsp[16] = "INF";
static char messageDsp[128] = "Welcome";
static char titleDsp[256] = "";
static int oldSize;
static void Usage()
{
(void) fprintf(stderr, "usage: xoct\n");
(void) fprintf(stderr,
"\t[-geometry [{width}][x{height}][{+-}{xoff}[{+-}{yoff}]]]\n");
(void) fprintf(stderr,
"\t[-display [{host}]:[{vs}]] [-fg {color}] [-bg {color}]\n");
(void) fprintf(stderr,
"\t[-{border|bd} {color}] [-mono] [-{size {int} | sticky}]\n");
(void) fprintf(stderr,
"\t[-{mode {int} | both}] [-[no]orient] [-[no]practice]\n");
(void) fprintf(stderr,
"\t[-face{0|1|2|3|4|5|6|7} {color}]\n");
exit(1);
}
static XrmOptionDescRec options[] = {
{"-fg", "*oct.Foreground", XrmoptionSepArg, NULL},
{"-bg", "*Background", XrmoptionSepArg, NULL},
{"-foreground", "*oct.Foreground", XrmoptionSepArg, NULL},
{"-background", "*Background", XrmoptionSepArg, NULL},
{"-border", "*oct.pieceBorder", XrmoptionSepArg, NULL},
{"-bd", "*oct.pieceBorder", XrmoptionSepArg, NULL},
{"-mono", "*oct.mono", XrmoptionNoArg, "TRUE"},
{"-size", "*oct.size", XrmoptionSepArg, NULL},
{"-sticky", "*oct.sticky", XrmoptionNoArg, "FALSE"},
{"-mode", "*oct.mode", XrmoptionSepArg, NULL},
{"-both", "*oct.mode", XrmoptionNoArg, "4"},
{"-orient", "*oct.orient", XrmoptionNoArg, "TRUE"},
{"-noorient", "*oct.orient", XrmoptionNoArg, "FALSE"},
{"-practice", "*oct.practice", XrmoptionNoArg, "TRUE"},
{"-nopractice", "*oct.practice", XrmoptionNoArg, "FALSE"},
{"-face0", "*oct.faceColor0", XrmoptionSepArg, NULL},
{"-face1", "*oct.faceColor1", XrmoptionSepArg, NULL},
{"-face2", "*oct.faceColor2", XrmoptionSepArg, NULL},
{"-face3", "*oct.faceColor3", XrmoptionSepArg, NULL},
{"-face4", "*oct.faceColor4", XrmoptionSepArg, NULL},
{"-face5", "*oct.faceColor5", XrmoptionSepArg, NULL},
{"-face6", "*oct.faceColor6", XrmoptionSepArg, NULL},
{"-face7", "*oct.faceColor7", XrmoptionSepArg, NULL}
};
int main(argc, argv)
int argc;
char *argv[];
{
Widget toplevel, oct;
toplevel = XtInitialize(argv[0], "Oct",
options, XtNumber(options), &argc, argv);
if (argc != 1)
Usage();
XtSetArg(arg[0], XtNiconPixmap,
XCreateBitmapFromData(XtDisplay(toplevel),
RootWindowOfScreen(XtScreen(toplevel)),
(char *) oct_bits, oct_width, oct_height));
XtSetValues(toplevel, arg, 1);
oct = XtCreateManagedWidget("oct", octWidgetClass, toplevel,
NULL, 0);
XtAddCallback(oct, XtNselectCallback, CallbackOct, NULL);
Initialize(oct);
XtRealizeWidget(toplevel);
XGrabButton(XtDisplay(oct), AnyButton, AnyModifier, XtWindow(oct),
TRUE, ButtonPressMask | ButtonMotionMask | ButtonReleaseMask,
GrabModeAsync, GrabModeAsync, XtWindow(oct),
XCreateFontCursor(XtDisplay(oct), XC_crosshair));
XtMainLoop();
#ifdef VMS
return 1;
#else
return 0;
#endif
}
static void Initialize(w)
Widget w;
{
int size, mode;
Boolean orient, sticky, practice;
XtVaSetValues(w,
XtNstart, FALSE,
NULL);
XtVaGetValues(w,
XtNsize, &size,
XtNmode, &mode,
XtNorient, &orient,
XtNsticky, &sticky,
XtNpractice, &practice,
NULL);
ReadRecords();
PrintRecord(size, mode, orient, sticky, practice, recordDsp);
oldSize = size;
PrintState(XtParent(w), progDsp, mode, size, sticky, movesDsp,
recordDsp, messageDsp);
}
static void CallbackOct(w, clientData, callData)
Widget w;
caddr_t clientData;
octCallbackStruct *callData;
{
int size, mode;
Boolean orient, sticky, practice, start;
XtVaGetValues(w,
XtNsize, &size,
XtNorient, &orient,
XtNmode, &mode,
XtNorient, &orient,
XtNsticky, &sticky,
XtNpractice, &practice,
XtNstart, &start,
NULL);
(void) strcpy(messageDsp, "");
switch (callData->reason) {
case OCT_RESTORE:
if (practice)
(void) strcpy(recordDsp, "practice");
movesDsp = 0;
break;
case OCT_RESET:
movesDsp = 0;
break;
case OCT_AMBIGUOUS:
(void) strcpy(messageDsp, "Ambiguous move");
break;
case OCT_ILLEGAL:
if (practice || start)
(void) strcpy(messageDsp, "Illegal move");
else
(void) strcpy(messageDsp, "Randomize to start");
break;
case OCT_MOVED:
movesDsp++;
#ifdef DEBUG
if (movesDsp > 256)
exit(1);
#endif
XtSetArg(arg[0], XtNstart, TRUE);
XtSetValues(w, arg, 1);
break;
case OCT_CONTROL:
return;
case OCT_SOLVED:
if (practice)
movesDsp = 0;
else {
if (HandleSolved(movesDsp, size, mode, orient, sticky))
(void) sprintf(messageDsp, "Congratulations %s!!", getlogin());
else
(void) strcpy(messageDsp, "Solved!");
}
XtSetArg(arg[0], XtNstart, FALSE);
XtSetValues(w, arg, 1);
break;
case OCT_PRACTICE:
movesDsp = 0;
practice = !practice;
if (!practice)
(void) strcpy(messageDsp, "Randomize to start");
PrintRecord(size, mode, orient, sticky, practice, recordDsp);
XtSetArg(arg[0], XtNpractice, practice);
XtSetArg(arg[1], XtNstart, FALSE);
XtSetValues(w, arg, 2);
break;
case OCT_RANDOMIZE:
movesDsp = 0;
XtSetArg(arg[0], XtNpractice, FALSE);
XtSetArg(arg[1], XtNstart, FALSE);
XtSetValues(w, arg, 2);
break;
case OCT_DEC:
if (!sticky) {
movesDsp = 0;
size--;
oldSize = size;
PrintRecord(size, mode, orient, sticky, practice, recordDsp);
XtSetArg(arg[0], XtNsize, size);
XtSetValues(w, arg, 1);
}
break;
case OCT_ORIENT:
movesDsp = 0;
orient = !orient;
PrintRecord(size, mode, orient, sticky, practice, recordDsp);
XtSetArg(arg[0], XtNorient, orient);
XtSetValues(w, arg, 1);
break;
case OCT_INC:
if (!sticky) {
movesDsp = 0;
size++;
oldSize = size;
PrintRecord(size, mode, orient, sticky, practice, recordDsp);
XtSetArg(arg[0], XtNsize, size);
XtSetValues(w, arg, 1);
}
break;
case OCT_PERIOD3:
case OCT_PERIOD4:
case OCT_BOTH:
movesDsp = 0;
mode = callData->reason - OCT_PERIOD3 + PERIOD3;
PrintRecord(size, mode, orient, sticky, practice, recordDsp);
XtSetArg(arg[0], XtNmode, mode);
XtSetValues(w, arg, 1);
break;
case OCT_STICKY:
movesDsp = 0;
sticky = !sticky;
if (sticky)
size = 4;
else
size = oldSize;
PrintRecord(size, mode, orient, sticky, practice, recordDsp);
XtSetArg(arg[0], XtNsticky, sticky);
XtSetArg(arg[1], XtNsize, size);
XtSetValues(w, arg, 2);
break;
case OCT_COMPUTED:
XtSetArg(arg[0], XtNstart, FALSE);
XtSetValues(w, arg, 1);
break;
case OCT_UNDO:
movesDsp--;
XtSetArg(arg[0], XtNstart, TRUE);
XtSetValues(w, arg, 1);
break;
}
PrintState(XtParent(w), progDsp, mode, size, sticky, movesDsp,
recordDsp, messageDsp);
}
static void PrintRecord(size, mode, orient, sticky, practice, record)
int size, mode;
Boolean orient, sticky, practice;
char *record;
{
int i = mode - PERIOD3;
int j = (orient) ? 1 : 0;
int k = (sticky) ? MAXOCTAS - MINOCTAS + 1 : size - MINOCTAS;
if (practice)
(void) strcpy(record, "practice");
else if (!sticky && size > MAXOCTAS)
(void) strcpy(record, "NOT RECORDED");
else if (octRecord[i][j][k] >= MAXRECORD)
(void) strcpy(record, "NEVER");
else
(void) sprintf(record, "%d", octRecord[i][j][k]);
}
static int HandleSolved(counter, size, mode, orient, sticky)
int counter, size, mode;
Boolean orient, sticky;
{
int i = mode - PERIOD3;
int j = (orient) ? 1 : 0;
int k = (sticky) ? MAXOCTAS - MINOCTAS + 1 : size - MINOCTAS;
if ((sticky || size <= MAXOCTAS) && counter < octRecord[i][j][k]) {
octRecord[i][j][k] = counter;
if ((size < 2 && mode != PERIOD4) || (size < 4 && mode == PERIOD4) ||
(orient && (octRecord[i][j][k] < octRecord[i][!j][k])))
octRecord[i][!j][k] = counter;
WriteRecords();
(void) sprintf(recordDsp, "%d", counter);
return TRUE;
}
return FALSE;
}
static void PrintState(w, prog, mode, size, sticky, moves, record, message)
Widget w;
char *prog, *record, *message;
int mode, size, moves;
Boolean sticky;
{
char ss[10], mb[10];
if (sticky)
(void) strcpy(ss, "sticky");
else
(void) sprintf(ss, "%d", size);
if (mode == BOTH)
(void) strcpy(mb, "both");
else
(void) sprintf(mb, "%d", mode);
(void) sprintf(titleDsp, "%s.%s: %s@ (%d/%s) - %s", prog, mb, ss, moves,
record, message);
XtSetArg(arg[0], XtNtitle, titleDsp);
XtSetValues(w, arg, 1);
}
static void ReadRecords()
{
FILE *fp;
int i, n, mode, orient;
for (mode = 0; mode < MAXMODES; mode++)
for (orient = 0; orient < 2; orient++)
for (i = 0; i <= MAXOCTAS - MINOCTAS + 1; i++)
octRecord[mode][orient][i] = MAXRECORD;
if ((fp = fopen(SCOREFILE, "r")) == NULL)
(void) sprintf(messageDsp, "Can not open %s, taking defaults.", SCOREFILE);
else {
for (mode = 0; mode < MAXMODES; mode++)
for (orient = 0; orient < 2; orient++)
for (i = 0; i <= MAXOCTAS - MINOCTAS + 1; i++) {
(void) fscanf(fp, "%d", &n);
octRecord[mode][orient][i] = n;
}
(void) fclose(fp);
}
}
static void WriteRecords()
{
FILE *fp;
int i, mode, orient;
if ((fp = fopen(SCOREFILE, "w")) == NULL)
(void) sprintf(messageDsp, "Can not write to %s.", SCOREFILE);
else {
for (mode = 0; mode < MAXMODES; mode++) {
for (orient = 0; orient < 2; orient++) {
for (i = 0; i <= MAXOCTAS - MINOCTAS + 1; i++)
(void) fprintf(fp, "%d ", octRecord[mode][orient][i]);
(void) fprintf(fp, "\n");
}
(void) fprintf(fp, "\n");
}
(void) fclose(fp);
}
}